/*
 * RTSPTablePanel.java
 *
 * Created on June 29, 2008, 3:31 PM
 */
package pl.edu.agh.ssm.monitor.gui;

import java.awt.Component;
import java.awt.Insets;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import pl.edu.agh.ssm.monitor.data.RTSPAnswer;
import pl.edu.agh.ssm.monitor.data.RTSPPacket;
import pl.edu.agh.ssm.monitor.data.SessionConnection;
import pl.edu.agh.ssm.monitor.utils.IconUtil;

/**
 *
 * @author  aneezka
 */
@SuppressWarnings("serial")
public class RTSPTablePanel extends javax.swing.JPanel implements UpdatePanel {

    /** Creates new form RTSPTablePanel */
    public RTSPTablePanel(SessionConnection connection) {
        this.connection = connection;
        this.iu = new IconUtil();
        initComponents();
        update();
    }

    @Override
    public String getName() {
        return "RTSP Packets";
    }

    public void update() {
        List<RTSPPacket> packets = connection.getRtspPackets();
        for (int i = currSize; i < packets.size(); i++) {
            RTSPPacket packet = packets.get(i);
            ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{iu.getRTSPIcon(packet), packet.getType(), packet.getRtspVersion(), packet.getTypeAttr(), packet.getCSeq(), packet.getPacketContent()});
            allPackets.add(packet);
            RTSPAnswer answer = packet.getAnswer();
            if (answer != null) {
                ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{iu.getRTSPAnswerIcon(answer), answer.getAnswerCode(), answer.getProtocol(), "", answer.getCSeq(), answer.getAnswerText()});
                allPackets.add(answer);
            }
        }
        currSize = packets.size();
        for (UpdatePanelListener l : listeners) {
            l.panelUpdated(this);
        }
    }

    public void remove() {
        for (UpdatePanelListener l : listeners) {
            l.panelRemoved(this);
        }
    }

    public void addListener(UpdatePanelListener listener) {
        listeners.add(listener);
    }

    public void removeListener(UpdatePanelListener listener) {
        listeners.remove(listener);
    }

    public RTSPPacket getSelectedPacket() {
//        ((DefaultTableModel)jTable1.getModel()).get
        int index = jTable1.getSelectedRow();
        if (index >= 0) {
            if (allPackets.get(index) instanceof RTSPPacket) {
                return (RTSPPacket) allPackets.get(index);
            } else if (allPackets.get(index) instanceof RTSPAnswer) {
                return (RTSPPacket) allPackets.get(index - 1);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public SessionConnection getConnection() {
        return connection;
    }

    private class IconCellRenderer extends JLabel implements TableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof Icon) {
                setIcon((Icon) value);
                setText(null);
            } else {
                setIcon(null);
                setText(value.toString());
            }

            Insets insets = getInsets();
            int w = getPreferredSize().width + insets.left + insets.right;
            TableColumn col = table.getColumnModel().getColumn(column);
            col.setPreferredWidth(Math.max(col.getPreferredWidth(), w));

            return this;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    @SuppressWarnings("serial")
	private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", "Type", "Protocol", "Request URL", "Cseq", "Content"
            }
        ) {
            Class<?>[] types = new Class<?> [] {
                javax.swing.Icon.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class<?> getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setDefaultRenderer(Icon.class, new IconCellRenderer());
        jTable1.getColumnModel().getColumn(0).setMaxWidth(20);
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    private SessionConnection connection;
    private int currSize = 0;
    private IconUtil iu;
    private List<UpdatePanelListener> listeners = new LinkedList<UpdatePanelListener>();
    private List<Object> allPackets = new LinkedList<Object>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
